//
//  CYAppDelegate.m
//  cyjf
//
//  Created by tauCross on 2017/4/17.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYAppDelegate.h"

#import "CYMainController.h"

#import "CYAppLauncher.h"

#import "CYActionManager.h"
#import "CYShareManager.h"
#import "CYGlobal.h"

#import <JPUSHService.h>
#import <TCJSONKit.h>



@implementation CYAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[CYDustManager standardManager] dustMsg];
    [CYAppLauncher startWithOptions:launchOptions];
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [[CYDustManager standardManager] dustMsg];
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler
{
    [[CYDustManager standardManager] dustMsg];
    [JPUSHService handleRemoteNotification:userInfo];
    if(application.applicationState != UIApplicationStateActive)
    {
        if([userInfo[@"action"] isKindOfClass:[NSString class]])
        {
            CYDataAPIActionModel *action = [CYDataAPIActionModel mj_objectWithKeyValues:[userInfo[@"action"] tc_JSONObject]];
            [CYStandardActionManager executeActionWithModel:action fromViewController:[CYMainController standardController]];
        }
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [[CYDustManager standardManager] dustMsg];
    [JPUSHService resetBadge];
    application.applicationIconBadgeNumber = 0;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    [[CYDustManager standardManager] dustMsg];
    switch(CYSharedGlobal.outsideSource)
    {
        case CYApplicationOutsiteSourceShare:
        {
            [[CYShareManager standardManager] handleOpenURL:url];
            break;
        }
    }
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    [[CYDustManager standardManager] dustMsg];
    switch(CYSharedGlobal.outsideSource)
    {
        case CYApplicationOutsiteSourceShare:
        {
            [[CYShareManager standardManager] handleOpenURL:url];
            break;
        }
    }
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    [[CYDustManager standardManager] dustMsg];
    switch(CYSharedGlobal.outsideSource)
    {
        case CYApplicationOutsiteSourceShare:
        {
            [[CYShareManager standardManager] handleOpenURL:url];
            break;
        }
    }
    return YES;
}

@end
